<?php

namespace app\agent\model;

use think\Model;

class Record extends Model
{


    // 表名
    protected $name = 'wallet_record';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'pay_status_text',
        'pay_time_text',
        'type_name'
    ];

    public function user()
    {
        return $this->hasOne('app\agent\model\User','id','user_id')->setEagerlyType(0);;
    }

    public function log()
    {
        return $this->hasOne('app\common\model\WalletLog','record_sn','record_sn');
    }
    
    public function getPayStatusList()
    {
        return ['0' => __('Pay_status 0'), '1' => __('Pay_status 1'), '-1' => __('Pay_status -1')];
    }     


    public function getPayStatusTextAttr($value, $data)
    {        
        $value = $value ? $value : (isset($data['pay_status']) ? $data['pay_status'] : '');
        $list = $this->getPayStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getTypeNameAttr($value, $data)
    {
        switch ($data['type']){
            case 1:
                return '充值';
                break;
            case 2:
                return '提现';
                break;
            case 3:
                return '扣除';
                break;
        }
    }

    public function getPayTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['pay_time']) ? $data['pay_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setPayTimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }



}
